package com.aistar.serviceVO.impl;

import com.aistar.mapper.CustomerMapper;
import com.aistar.mapper.ProgramCommentMapper;
import com.aistar.mapper.ProgramMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.Program;
import com.aistar.pojo.ProgramComment;
import com.aistar.pojo.ProgramCommentExample;
import com.aistar.pojoVO.ProgramCommentVO;
import com.aistar.serviceVO.ProgramCommentVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProgramCommentVOServiceImpl implements ProgramCommentVOService {
    private final ProgramCommentMapper programCommentMapper;
    private final CustomerMapper customerMapper;
    private final ProgramMapper programMapper;

    @Autowired
    public ProgramCommentVOServiceImpl(
            ProgramCommentMapper programCommentMapper,
            CustomerMapper customerMapper,
            ProgramMapper programMapper) {
        this.programCommentMapper = programCommentMapper;
        this.customerMapper = customerMapper;
        this.programMapper = programMapper;
    }

    @Override
    public ServerResponse getByProgramId(Integer programId) {
        Program program = programMapper.selectByPrimaryKey(programId);
        if (program != null) {
            ProgramCommentExample example = new ProgramCommentExample();
            example.createCriteria()
                    .andProgramIdEqualTo(programId)
                    .andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
            example.setOrderByClause("program_comment_time desc");
            List<ProgramComment> programCommentList = programCommentMapper.selectByExample(example);
            if (programCommentList != null && programCommentList.size() > 0) {
                List<ProgramCommentVO> programCommentVOList = new ArrayList<>();
                for (ProgramComment programComment : programCommentList) {
                    Customer customer = customerMapper.selectByPrimaryKey(programComment.getCustomerId());
                    programCommentVOList.add(new ProgramCommentVO(programComment, program, customer));
                }
                return ServerResponse.getSuccess(programCommentVOList);
            }
        }
        return ServerResponse.getFailed();
    }
}
